import Vapor


/// Register your application's routes here.
public func routes(_ router: Router) throws {
    
    try setHomeRoutes(router)
    ///基础api
    let api = router.grouped("api")

    let v1 = api.grouped("v1")
    let v2 = api.grouped("v2")
    //认证模块不需要Token验证
    
    try setAuthRoutes(v1)
    try setAuthRoutes(v2)
    
    let token = v1.grouped(TokenMiddleware())
    let token2 = v2.grouped(TokenMiddleware())
    //其它几个模块都需要token认证
    try setTodoRoutes(token)
    try setTodoRoutes(token2)
}




fileprivate func setHomeRoutes(_ router: Router) throws {
    // Example of configuring a controller
    let homeController = HomeController()
    let home = router.grouped("home")
    home.get("index", use: homeController.index)
    home.get("index2", use: homeController.index2)
//    home.get("addTodoItem", use: todoController.index)
//    home.post("addTodoItem", use: todoController.create)
//    home.delete("delTodoItem", use: todoController.delete)
//    home.put("editTodoItem", use: todoController.update)
}

fileprivate func setTodoRoutes(_ router: Router) throws {
    // Example of configuring a controller
    let todoController = TodoController()
    let home = router.grouped("home")
        home.get("todos", use: todoController.index)
        home.post("addTodoItem", use: todoController.create)
        home.delete("delTodoItem", use: todoController.delete)
        home.put("editTodoItem", use: todoController.update)
}
fileprivate func setAuthRoutes(_ router: Router) throws {

    let authController = AuthController()
    let home = router.grouped("auth")
        home.post("register", use: authController.register)
        home.post("login", use: authController.login)
    
        home.get("register") { (req) -> EventLoopFuture<Response> in
            return try callFail("请求方式有误", status: 0, req: req)
        }

}
